﻿using System;
using System.IO;
using System.Text;
using Hongsh.Pay.Client.Proxy.Http;
using MapleLeaf.Pay;
using MapleLeaf.Pay.Pay;
using MapleLeaf.Pay.Trade;

namespace Hongsh.Pay.Client.Proxy.Handlers {
    // Token: 0x0200003D RID: 61
    public static class BestPayProxy {
        // Token: 0x060001C6 RID: 454 RVA: 0x0000335A File Offset: 0x0000155A
        public static void AddBestPayProxyHandler(this HttpServer svr) {
            svr.RouterList.Add("POST/mapi/aggregatepay/offline/b2c/pay", new HttpContextActionDelegate(Process));
            svr.RouterList.Add("POST/mapi/crm/tradeQuery", new HttpContextActionDelegate(Process));
        }

        // Token: 0x060001C7 RID: 455 RVA: 0x0000D108 File Offset: 0x0000B308
        private static void Process(HttpContext ctx) {
            BestPayRequest bestPayRequest = new StreamReader(ctx.Request).ReadToEnd().JsonDecode<BestPayRequest>();
            BestPayResponse bestPayResponse = new BestPayResponse();
            try {
                HongshPayTrade hongshPayTrade;
                if (!string.IsNullOrEmpty(bestPayRequest.mediumNo)) {
                    hongshPayTrade = ctx.Execute(new TradeAuthPaymentRequest {
                        Amount = bestPayRequest.tradeAmt,
                        Authcode = bestPayRequest.mediumNo,
                        ClientTradeId = bestPayRequest.outTradeNo
                    });
                    bestPayResponse.errorCode = "000000";
                    bestPayResponse.errorMsg = "成功";
                }
                else {
                    hongshPayTrade = ctx.Execute(new TradePaymentQueryRequest {
                        ClientTradeId = bestPayRequest.outTradeNo
                    });
                }
                bestPayResponse.result = new BestPayResult {
                    tradeNo = hongshPayTrade.TradeId,
                    outTradeNo = hongshPayTrade.ClientTradeId,
                    merchantNo = hongshPayTrade.TerminalId,
                    tradeAmt = hongshPayTrade.Amount,
                    buyerLoginNo = hongshPayTrade.Payer,
                    tradeType = (hongshPayTrade.Payway == TradePayway.微信) ? null : "ALIPAY"
                };
                if (hongshPayTrade.State == TradeState.成功) {
                    bestPayResponse.success = true;
                    bestPayResponse.result.payAmt = hongshPayTrade.Amount;
                    bestPayResponse.result.tradeStatus = "SUCCESS";
                    bestPayResponse.result.tradeResultCode = "SUCCESS";
                    bestPayResponse.result.tradeResultDesc = "交易成功";
                    bestPayResponse.result.tradeFinishedDate = hongshPayTrade.UpdateTime.ToString("s");
                }
                else if (hongshPayTrade.State == TradeState.失败 || hongshPayTrade.State == TradeState.关闭) {
                    bestPayResponse.result.tradeStatus = "FAIL";
                    bestPayResponse.result.tradeResultCode = "FAIL";
                    bestPayResponse.result.tradeResultDesc = hongshPayTrade.StateDescript;
                }
                else {
                    bestPayResponse.success = true;
                    bestPayResponse.result.tradeStatus = "WAITFORPAY";
                }
            }
            catch (Exception ex) {
                bestPayResponse.errorCode = "TRADE_STATUS_ERROR";
                bestPayResponse.errorMsg = ex.Message;
            }
            bestPayResponse.sign = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString("N")).Base64Encode();
            ctx.Result = new HttpJsonResult(bestPayResponse);
        }

        // Token: 0x0200003E RID: 62
        public class BestPayRequest {
            // Token: 0x170000BD RID: 189
            // (get) Token: 0x060001C8 RID: 456 RVA: 0x00003394 File Offset: 0x00001594
            // (set) Token: 0x060001C9 RID: 457 RVA: 0x0000339C File Offset: 0x0000159C
            public int tradeAmt { get; set; }

            // Token: 0x170000BE RID: 190
            // (get) Token: 0x060001CA RID: 458 RVA: 0x000033A5 File Offset: 0x000015A5
            // (set) Token: 0x060001CB RID: 459 RVA: 0x000033AD File Offset: 0x000015AD
            public string outTradeNo { get; set; }

            // Token: 0x170000BF RID: 191
            // (get) Token: 0x060001CC RID: 460 RVA: 0x000033B6 File Offset: 0x000015B6
            // (set) Token: 0x060001CD RID: 461 RVA: 0x000033BE File Offset: 0x000015BE
            public string mediumNo { get; set; }

            // Token: 0x170000C0 RID: 192
            // (get) Token: 0x060001CE RID: 462 RVA: 0x000033C7 File Offset: 0x000015C7
            // (set) Token: 0x060001CF RID: 463 RVA: 0x000033CF File Offset: 0x000015CF
            public string merchantNo { get; set; }

            // Token: 0x060001D0 RID: 464 RVA: 0x0000286D File Offset: 0x00000A6D
            public BestPayRequest() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }

        // Token: 0x0200003F RID: 63
        public class BestPayResponse {
            // Token: 0x170000C1 RID: 193
            // (get) Token: 0x060001D1 RID: 465 RVA: 0x000033D8 File Offset: 0x000015D8
            // (set) Token: 0x060001D2 RID: 466 RVA: 0x000033E0 File Offset: 0x000015E0
            public string errorCode { get; set; }

            // Token: 0x170000C2 RID: 194
            // (get) Token: 0x060001D3 RID: 467 RVA: 0x000033E9 File Offset: 0x000015E9
            // (set) Token: 0x060001D4 RID: 468 RVA: 0x000033F1 File Offset: 0x000015F1
            public string errorMsg { get; set; }

            // Token: 0x170000C3 RID: 195
            // (get) Token: 0x060001D5 RID: 469 RVA: 0x000033FA File Offset: 0x000015FA
            // (set) Token: 0x060001D6 RID: 470 RVA: 0x00003402 File Offset: 0x00001602
            public BestPayResult result { get; set; }

            // Token: 0x170000C4 RID: 196
            // (get) Token: 0x060001D7 RID: 471 RVA: 0x0000340B File Offset: 0x0000160B
            // (set) Token: 0x060001D8 RID: 472 RVA: 0x00003413 File Offset: 0x00001613
            public string sign { get; set; }

            // Token: 0x170000C5 RID: 197
            // (get) Token: 0x060001D9 RID: 473 RVA: 0x0000341C File Offset: 0x0000161C
            // (set) Token: 0x060001DA RID: 474 RVA: 0x00003424 File Offset: 0x00001624
            public bool success { get; set; }

            // Token: 0x060001DB RID: 475 RVA: 0x0000286D File Offset: 0x00000A6D
            public BestPayResponse() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }

        // Token: 0x02000040 RID: 64
        public class BestPayResult {
            // Token: 0x170000C6 RID: 198
            // (get) Token: 0x060001DC RID: 476 RVA: 0x0000342D File Offset: 0x0000162D
            // (set) Token: 0x060001DD RID: 477 RVA: 0x00003435 File Offset: 0x00001635
            public string outTradeNo { get; set; }

            // Token: 0x170000C7 RID: 199
            // (get) Token: 0x060001DE RID: 478 RVA: 0x0000343E File Offset: 0x0000163E
            // (set) Token: 0x060001DF RID: 479 RVA: 0x00003446 File Offset: 0x00001646
            public string buyerLoginNo { get; set; }

            // Token: 0x170000C8 RID: 200
            // (get) Token: 0x060001E0 RID: 480 RVA: 0x0000344F File Offset: 0x0000164F
            public string buyerContractNo {
                get {
                    return buyerLoginNo;
                }
            }

            // Token: 0x170000C9 RID: 201
            // (get) Token: 0x060001E1 RID: 481 RVA: 0x00003457 File Offset: 0x00001657
            public string merchantOrderNo {
                get {
                    return outTradeNo;
                }
            }

            // Token: 0x170000CA RID: 202
            // (get) Token: 0x060001E2 RID: 482 RVA: 0x0000345F File Offset: 0x0000165F
            // (set) Token: 0x060001E3 RID: 483 RVA: 0x00003467 File Offset: 0x00001667
            public string merchantNo { get; set; }

            // Token: 0x170000CB RID: 203
            // (get) Token: 0x060001E4 RID: 484 RVA: 0x00003470 File Offset: 0x00001670
            public string merchantName {
                get {
                    return merchantNo;
                }
            }

            // Token: 0x170000CC RID: 204
            // (get) Token: 0x060001E5 RID: 485 RVA: 0x00003470 File Offset: 0x00001670
            public string @operator {
                get {
                    return merchantNo;
                }
            }

            // Token: 0x170000CD RID: 205
            // (get) Token: 0x060001E6 RID: 486 RVA: 0x00003478 File Offset: 0x00001678
            // (set) Token: 0x060001E7 RID: 487 RVA: 0x00003480 File Offset: 0x00001680
            public string closeFlag { get; set; }

            // Token: 0x170000CE RID: 206
            // (get) Token: 0x060001E8 RID: 488 RVA: 0x00003489 File Offset: 0x00001689
            // (set) Token: 0x060001E9 RID: 489 RVA: 0x00003491 File Offset: 0x00001691
            public string refundFlag { get; set; }

            // Token: 0x170000CF RID: 207
            // (get) Token: 0x060001EA RID: 490 RVA: 0x0000349A File Offset: 0x0000169A
            // (set) Token: 0x060001EB RID: 491 RVA: 0x000034A2 File Offset: 0x000016A2
            public string discountAmt { get; set; }

            // Token: 0x170000D0 RID: 208
            // (get) Token: 0x060001EC RID: 492 RVA: 0x000034AB File Offset: 0x000016AB
            // (set) Token: 0x060001ED RID: 493 RVA: 0x000034B3 File Offset: 0x000016B3
            public int tradeAmt { get; set; }

            // Token: 0x170000D1 RID: 209
            // (get) Token: 0x060001EE RID: 494 RVA: 0x000034BC File Offset: 0x000016BC
            // (set) Token: 0x060001EF RID: 495 RVA: 0x000034C4 File Offset: 0x000016C4
            public int payAmt { get; set; }

            // Token: 0x170000D2 RID: 210
            // (get) Token: 0x060001F0 RID: 496 RVA: 0x000034CD File Offset: 0x000016CD
            public string subject { get; }

            // Token: 0x170000D3 RID: 211
            // (get) Token: 0x060001F1 RID: 497 RVA: 0x000034D5 File Offset: 0x000016D5
            public string bankRequestNo {
                get {
                    return tradeNo;
                }
            }

            // Token: 0x170000D4 RID: 212
            // (get) Token: 0x060001F2 RID: 498 RVA: 0x000034D5 File Offset: 0x000016D5
            public string tradeprodNo {
                get {
                    return tradeNo;
                }
            }

            // Token: 0x170000D5 RID: 213
            // (get) Token: 0x060001F3 RID: 499 RVA: 0x000034DD File Offset: 0x000016DD
            // (set) Token: 0x060001F4 RID: 500 RVA: 0x000034E5 File Offset: 0x000016E5
            public string tradeNo { get; set; }

            // Token: 0x170000D6 RID: 214
            // (get) Token: 0x060001F5 RID: 501 RVA: 0x000034EE File Offset: 0x000016EE
            // (set) Token: 0x060001F6 RID: 502 RVA: 0x000034F6 File Offset: 0x000016F6
            public string tradeType { get; set; }

            // Token: 0x170000D7 RID: 215
            // (get) Token: 0x060001F7 RID: 503 RVA: 0x000034FF File Offset: 0x000016FF
            // (set) Token: 0x060001F8 RID: 504 RVA: 0x00003507 File Offset: 0x00001707
            public string tradeStatus { get; set; }

            // Token: 0x170000D8 RID: 216
            // (get) Token: 0x060001F9 RID: 505 RVA: 0x00003510 File Offset: 0x00001710
            // (set) Token: 0x060001FA RID: 506 RVA: 0x00003518 File Offset: 0x00001718
            public string tradeResultCode { get; set; }

            // Token: 0x170000D9 RID: 217
            // (get) Token: 0x060001FB RID: 507 RVA: 0x00003521 File Offset: 0x00001721
            // (set) Token: 0x060001FC RID: 508 RVA: 0x00003529 File Offset: 0x00001729
            public string tradeResultDesc { get; set; }

            // Token: 0x170000DA RID: 218
            // (get) Token: 0x060001FD RID: 509 RVA: 0x00003532 File Offset: 0x00001732
            // (set) Token: 0x060001FE RID: 510 RVA: 0x0000353A File Offset: 0x0000173A
            public string tradeFinishedDate { get; set; }

            // Token: 0x170000DB RID: 219
            // (get) Token: 0x060001FF RID: 511 RVA: 0x00003543 File Offset: 0x00001743
            public string payFinishedDate {
                get {
                    return tradeFinishedDate;
                }
            }

            // Token: 0x06000200 RID: 512 RVA: 0x0000354B File Offset: 0x0000174B
            public BestPayResult() {
                //Class15.cwcZ3SWztHroi();
                closeFlag = "FALSE";
                refundFlag = "FALSE";
                discountAmt = "0";
                subject = "线下扫码收款";
                //base..ctor();
            }
        }
    }
}
